home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_04 / allison / retref.cpp < prev    next >
C/C++ Source or Header  |  1995-02-06  |  454b  |  30 lines

  1. LISTING 8 - Returns an object from a function by reference
  2.  
  3. //retref.cpp:   Returning a reference
  4. #include <stdio.h>
  5.  
  6. int & current();    // Returns a reference
  7.  
  8. int a[4] = {0,1,2,3};
  9. int index = 0;
  10.  
  11. main()
  12. {
  13.     current() = 10;
  14.     index = 3;
  15.     current() = 20;
  16.     for (int i = 0; i < 4; ++i)
  17.         printf("%d ",a[i]);
  18.     putchar('\n');
  19.     return 0;
  20. }
  21.  
  22. int & current()
  23. {
  24.     return a[index];
  25. }
  26.  
  27. // Output:
  28. 10 1 2 20
  29.  
  30.